package com.renwei.yunlong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.bean.DepartBean;
import com.renwei.yunlong.utils.StringUtils;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;

import io.github.xudaojie.qrcodelib.zxing.DensityUtils;

/**
 * Project :  yunaandroid.
 * Package name: com.renwei.yunlong.adapter
 * Created by :  Benjamin.
 * Created time: 2017/12/6 18:20
 * Changed by :  Benjamin.
 * Changed time: 2017/12/6 18:20
 * Class description:
 */

public class DepartmentSelectAdapter extends BaseRecyclerViewAdapter<DepartBean.RowsBean> {
    private String currentCheckId = "";

    public DepartmentSelectAdapter(Context mContext, String currentId) {
        super(mContext);
        this.currentCheckId = currentId;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_function_group_select, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder rholder, final int position) {
        ViewHolder holder = (ViewHolder) rholder;
        final DepartBean.RowsBean item = getItem(position);

        holder.tvFunctionName.setText(item.getDepartmentName());
        if (item.getIndex() == 1) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 0, 0, 0);
            holder.itemFunction.setLayoutParams(params);
        } else if (item.getIndex() == 2) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(DensityUtils.dp2px(mContext, 30), 0, 0, 0);
            holder.itemFunction.setLayoutParams(params);
        } else if (item.getIndex() == 3) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.setMargins(DensityUtils.dp2px(mContext, 60), 0, 0, 0);
            holder.itemFunction.setLayoutParams(params);
        }
        if (item.getDepartmentId().equals(currentCheckId)) {
            holder.ivCheck.setImageResource(R.mipmap.check02);
        } else {
            holder.ivCheck.setImageResource(R.mipmap.check03);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                currentCheckId = StringUtils.value(item.getDepartmentId());
                notifyDataSetChanged();
                mOnItemClickLitener.onItemClick(view, position);
            }
        });
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvFunctionName;
        ImageView ivCheck;
        LinearLayout itemFunction;

        public ViewHolder(View rootView) {
            super(rootView);
            this.tvFunctionName = rootView.findViewById(R.id.tv_function_name);
            this.itemFunction = rootView.findViewById(R.id.item_function);
            this.ivCheck = rootView.findViewById(R.id.iv_function_checke);
        }

    }
}
